package com.palazzisoft.lucene.dss;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

/**
 * 
 * @author pablo
 *
 */
public class LuceneDemo {

	public Analyzer analyzer = null;
	public IndexWriterConfig indexWriterConfig = null;
	public IndexWriter writer = null;
	public Directory dir = null;
	
	public LuceneDemo() throws IOException {
	    boolean create = true;
	    File indexDirFile = new File("/tmp/indexer");
	    if (indexDirFile.exists() && indexDirFile.isDirectory()) {
	       create = false;
	    }
	 
	    this.dir = FSDirectory.open(indexDirFile);
	    this.analyzer = new StandardAnalyzer(Version.LUCENE_4_9);
	    this.indexWriterConfig = new IndexWriterConfig(Version.LUCENE_4_9, analyzer);
	 
	    if (create) {
	       indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
	    }
	 
	    this.writer = new IndexWriter(dir, indexWriterConfig);
	    this.insertDocument(writer, "El amor en los tiempos del cólera", "4546677-2352");
	    writer.commit();
	    writer.close(true);
		
	}
	
	public static void main(String[] args) throws IOException, ParseException {	
		LuceneDemo luceneDemo = new LuceneDemo();
		luceneDemo.inocente("El amor en los tiempos del cólera");
	}
	
	public void inocente(String query) throws ParseException, IOException {
		Query q = new QueryParser(Version.LUCENE_4_9, "title", analyzer).parse(query);	
		
	    int hitsPerPage = 10;
	    IndexReader reader = DirectoryReader.open(this.dir);
	    IndexSearcher searcher = new IndexSearcher(reader);
	    TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
	    searcher.search(q, collector);
	    ScoreDoc[] hits = collector.topDocs().scoreDocs;	
	    
	    System.out.println("Found " + hits.length + " hits.");
	    for(int i=0;i<hits.length;++i) {
	      int docId = hits[i].doc;
	      Document d = searcher.doc(docId);
	      System.out.println((i + 1) + ". " + d.get("isbn") + "\t" + d.get("title"));
	    }

	    // reader can only be closed when there
	    // is no need to access the documents any more.
	    reader.close();	    
	}
	
	private void insertDocument(IndexWriter writer, String title, String isbn) throws IOException {
		Document doc = new Document();
		doc.add(new TextField("title", title, Field.Store.YES));
		doc.add(new TextField("isbn", isbn, Field.Store.YES));
		
		writer.addDocument(doc);
	}
}
